package cc.sjfx.sharedoc.widget.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import cn.mandroid.frame.toast.MToast;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bmob.FileInfo4Examination;
import cc.sjfx.sharedoc.utils.OpenFileUtils;
import cc.sjfx.sharedoc.utils.StrUtils;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileAdapter extends BaseAdapter {
	private List<FileInfo4Examination> list = new ArrayList<FileInfo4Examination>();
	private Context context;

	public FileAdapter(Context context, String file) {
		this.context = context;
		getFile(file);
	}

	private void getFile(String file) {
		// TODO Auto-generated method stub
		if (file == null) {
			return;
		}
		try {
			JSONArray jsonArray = new JSONArray(file);
			for (int i = 0; i < jsonArray.length(); i++) {
				FileInfo4Examination examination = new FileInfo4Examination();
				examination.setFileName(jsonArray.getJSONObject(i).getString(
						"fileName"));
				examination.setFileType(jsonArray.getJSONObject(i).getString(
						"fileType"));
				examination.setFileSize(jsonArray.getJSONObject(i).getLong(
						"fileSize"));
				examination.setFileNetName(jsonArray.getJSONObject(i)
						.getString("fileNetName"));
				list.add(examination);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final FileInfo4Examination info = list.get(arg0);
		arg1 = LayoutInflater.from(context).inflate(R.layout.listview_file,
				null);
		TextView fileNameText = (TextView) arg1.findViewById(R.id.fileNameText);
		fileNameText.setText(info.getFileName());
		ImageView fileImg = (ImageView) arg1.findViewById(R.id.fileImg);
		TextView sizeText = (TextView) arg1.findViewById(R.id.sizeText);
		if (new File(StrUtils.DOWNLOADPATH + "/" + info.getFileName()).exists()) {
			fileImg.setImageResource(R.drawable.finish_ok);
			sizeText.setText("已下载");
			arg1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = OpenFileUtils.getIntent(
							StrUtils.DOWNLOADPATH + "/" + info.getFileName(),
							info.getFileType());
					if (intent != null) {
						context.startActivity(intent);
					} else {
						MToast.show(context, "无法打开" + info.getFileType()
								+ "格式的文件，文件位置：" + StrUtils.DOWNLOADPATH + "/"
								+ info.getFileName());
					}

				}
			});
		} else {
			fileImg.setImageResource(R.drawable.download_black);
			double siz = info.getFileSize() / 1024 / 1024.0;
			int s = (int) (siz * 100);
			if (s > 10) {
				sizeText.setText((s / 100.0) + "M");
			} else {
				sizeText.setText(((int) (siz * 1024)) + "K");
			}
		}

		return arg1;
	}

}
